Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LibraryImportGenerator] Use basic forwarder in down-level support if any parameters can't be marshalled #104416

Merged
merged 4 commits into from
Jul 8, 2024

Conversation

elinor-fung
Copy link
Member

@elinor-fung elinor-fung commented Jul 4, 2024

Alternative to #103973 (and #104246)

In down-level support, when determining marshalling generators for types, we fall back to just forwarding the parameter without erroring. This means that we end up generating a stub that marshal some parameters and just silently forwards others when all of these are true:

  • target framework is down-level (below .NET 7)
  • the signature is not blittable (parameters require marshalling and/or SetLastError=true)
  • no parameters are identified via an ITypeBasedMarshallingInfoProvider as missing marshalling support
  • at least one parameter cannot be marshalled and falls back to just forwarding the parameter

This is not a problem in non-down-level scenarios, as we error if we can't marshal any parameters. This is also not a problem if any parameters are identified as unsupported via an ITypeBasedMarshallingInfoProvider, as we do correctly check that case and simply forward to DllImport.

This change makes it so that in down-level scenarios, if any type falls back to forwarding, we print a basic DllImport instead of a stub with mixed marshalling and forwarding.

We have existing tests for validating that any unsupported type resulted in a forwarder even when other types were supported:

// Confirm that if support is missing for any type (like arrays), we fall back to a forwarder even if other types are supported.
{
string code = CodeSnippets.BasicReturnAndParameterByValue("System.Runtime.InteropServices.SafeHandle", "int[]", CodeSnippets.LibraryImportAttributeDeclaration);
yield return new object[] { ID(), code, TestTargetFramework.Net6, true };

However, they only used types that were explicitly flagged as not supported by an ITypeBasedMarshallingInfoProvider that handled the type. They were also using SafeHandle as the non-blittable type that was supported down-level, but that is no longer the case in main (has been removed in .NET 9 so that we only supporting marshalling via SafeHandleMarshaller).

@elinor-fung
Copy link
Member Author

/azp run runtime-libraries-coreclr outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Co-authored-by: Aaron Robinson <arobins@microsoft.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants